package com.prs.crm.action.oa;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.courser.ui.TreeNode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.hr.Dept;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.oa.MyFileFolder;
import com.prs.crm.domain.oa.Notice;
import com.prs.crm.service.oa.MyFileFolderService;
import com.prs.crm.service.oa.MyFileService;

public class MyFileFolderAction extends BaseAction{
	private static final long serialVersionUID = 1L;
	private Integer[] ids;
	private PaginationSupport pagination;
	private MyFileFolder folder;
	private MyFileFolderService myFileFolderService;
	private MyFileService myFileService;
	private TreeNode root;
	
	@Action("listMyFileFolder")
	public String listMyFileFolder(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MyFileFolder.class);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("recorder", person));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMyFileFolderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action("selectFolder")
	public String selectFolder(){
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				MyFileFolder.class);
		Person person = AppContext.getAccountContext().currentUser();
		criteria.add(Restrictions.eq("recorder", person));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getMyFileFolderService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	
	@Action(value = "loadMyFileFolderTree", results = { @Result(name = "success", type = "json", params = {
			"root", "root" }) })
	public String loadTree() {
		root = new TreeNode();
		root.setId("-1");

		TreeNode firstNode = new TreeNode();
		firstNode.setId("0");
		firstNode.setText("文件夹");

		Collection<MyFileFolder> folders = this.getMyFileFolderService().getAllFolder();

		for (MyFileFolder folder : folders) {
			TreeNode node = new TreeNode();
			node.setText(folder.getTitle());
			node.setId("fileFolder-" + folder.getId().toString());
			firstNode.addChildNode(node);
		}
		root.addChildNode(firstNode);

		return SUCCESS;
	}
	
	@Action("editMyFileFolder")
	public String editMyFileFolder(){
		return SUCCESS;
	}
	
	@Action("viewMyFileFolder")
	public String viewMyFileFolder(){
		return SUCCESS;
	}
	
	@Action(value = "saveMyFileFolder", results = {
			@Result(name = "success", type = "redirect", location = "listMyFileFolder"),
			@Result(name = "error", type = "chain", location = "editMyFileFolder") })
	public String save() {
		if (folder.getTitle() == null || "".equals(folder.getTitle().trim())) {
			addActionError("标题不能为空");
			return ERROR;
		}
		this.setPrompt("创建文件夹成功");
		Person person = AppContext.getAccountContext().currentUser();
		folder.setRecorder(person);
		this.getMyFileFolderService().save(folder);
		return SUCCESS;
	}
	
	@Action(value = "removeMyFileFolder", results = {
			@Result(name = "success", type = "redirect", location = "listMyFileFolder"),
			@Result(name = "error", type = "chain", location = "listMyFileFolder") })
	public String remove() {
		if (ids == null) {
			this.addActionError("请选择要删除的文件夹信息");
			return ERROR;
		}
		for (Integer id : ids) {
			MyFileFolder folder = this.getMyFileFolderService().get(id);
			if (this.getMyFileService().getMyFileByFolder(folder).size() != 0) {
				this.addActionError("该文件夹包含文件，不能删除");
				return ERROR;
			}
		}
		this.getMyFileFolderService().batchRemove(ids);
		return SUCCESS;
	}
	
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public MyFileFolder getFolder() {
		return folder;
	}
	public void setFolder(MyFileFolder folder) {
		this.folder = folder;
	}
	public MyFileFolderService getMyFileFolderService() {
		return myFileFolderService;
	}
	public void setMyFileFolderService(MyFileFolderService myFileFolderService) {
		this.myFileFolderService = myFileFolderService;
	}

	public MyFileService getMyFileService() {
		return myFileService;
	}

	public void setMyFileService(MyFileService myFileService) {
		this.myFileService = myFileService;
	}

	public TreeNode getRoot() {
		return root;
	}

	public void setRoot(TreeNode root) {
		this.root = root;
	}
}
